import axios from 'axios'
const baseUrl = 'http://localhost:8080'

const http = axios.create({
  baseURL: baseUrl,
})


http.interceptors.response.use(response => {
  return response.data
})

/**
 * 根据文件的md5获取未上传完的任务
 * @param identifier 文件md5
 * @returns {Promise<AxiosResponse<any>>}
 */
export const taskInfo = (identifier: string) => {
  return http.get(`/v1/minio/tasks/${identifier}`)
}

export type InitTaskReq = {
  identifier: string,
  fileName: string,
  totalSize: number,
  chunkSize: number
}

/**
 * 初始化一个分片上传任务
 * @param identifier 文件md5
 * @param fileName 文件名称
 * @param totalSize 文件大小
 * @param chunkSize 分块大小
 * @returns {Promise<AxiosResponse<any>>}
 */
export const initTask = ({ identifier, fileName, totalSize, chunkSize }: InitTaskReq) => {
  return http.post('/file/multipart/init', { identifier, fileName, totalSize, chunkSize })
}

export type PreSignUrlReq = {
 identifier: string, 
 partNumber: number 
}
/**
 * 获取预签名分片上传地址
 * @param identifier 文件md5
 * @param partNumber 分片编号
 * @returns {Promise<AxiosResponse<any>>}
 */
export const preSignUrl = ({ identifier, partNumber }: PreSignUrlReq) => {
  return http.get(`/v1/minio/tasks/${identifier}/${partNumber}`)
}

/**
 * 合并分片
 * @param identifier
 * @returns {Promise<AxiosResponse<any>>}
 */
export const merge = (identifier: string) => {
  return http.post(`/v1/minio/tasks/merge/${identifier}`)
}


